import { Notify } from 'vant';
// 校验手机号
const phoneReg = 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/;
// 校验手机号
const validateTel = /^1(3|4|5|6|7|8|9)\d{9}$/;
// 校验身份证
const idCardReg = /^[1-9]\d{1}(.{13}|.{16})$/;
const checkPhone = (params) => {
  if (!params || params === '') {
    Notify('请输入手机号!');
  } else if (!phoneReg.test(params)) {
    Notify('手机格式不正确!');
    return false;
  } else {
    return true;
  }
};
const checkIdCard = (params) => {
  if (!params || params === '') {
    Notify('请输入身份证号!');
  } else if (idCardReg.test(params)) {
    if (params.length == 18) {
      //加权因子
      let weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
      // 校验位
      let check_code = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
      let idWiSum = 0;
      for (let i = 0; i < 17; i++) {
        idWiSum += params.substring(i, i + 1) * weight_factor[i];
      }
      //计算模
      let idMod = idWiSum % 11;
      let idLastCode = check_code[idMod];
      if (params[17] == idLastCode) {
        return true;
      }
      Notify('身份证最后一位校验码不正确');
      return false;
    }
  } else {
    Notify('身份证仅能为15位或18位且前两位为有效行政区划代码');
    return false;
  }
};
// 简单的封装必填
const validateForm = (rules, object) => {
  for (const key in rules) {
    object[key] = object[key] === 0 ? '0' : object[key];
    // 判断是否为空
    if (!object[key] || object[key] === '') {
      if (rules[key].required) Notify(rules[key].message);
      return false;
    }
  }
  return true;
};

export { checkPhone, checkIdCard, validateTel, validateForm };
